﻿using System;
using System.Collections;
using System.Collections.Generic;
using TrueSync;
using Network;
using Framework;


public enum FlySubitemInitPositionEnum
{
    OnMyEntity,
    OnRivalsEntity,
    OnTargetPosition,
    OnUseParentHangpoint,
    OnUseCollisionPosition,
}
[System.Serializable]
public class LogicFlySubItemMoveComponent : LogicComponent
{
   
    public bool AllowPenetrateThrowPositionRect;
    public OnUserParentHangpointDef OnUserParentHangpoint;
    public FlySubitemInitPositionEnum FlySubitemInitPosition;
    public HangpointDef Hangpoint;
    public FP InitialSpeed;
    public FP XAcceleration;
    public FP YAcceleration;
    public bool AllowBounceBack;
    public bool AllowPenetrateThroughPositionRect;
    public bool AllowFlyOutsideScene;

    LogicEntity mLEntity;
    bool mIsInit;

    TSVector2 mPosition;
    public TSVector2 Position
    {
        get
        {
            return mPosition;
        }
        set
        {
            mPosition = value;
            mLEntity.SendEntityMessage((int)L2VMsgId.SetPosition, mPosition);
        }
    }
    int mDirection;
    TSVector2 mMoveSpeed;

    public void Init(LogicEntity LEntity)
    {
        mLEntity = LEntity;
        mIsInit = false;
    }

    public void Start()
    {
        TSVector2 initPosition = TSVector2.zero;
        switch(FlySubitemInitPosition)
        {
            case FlySubitemInitPositionEnum.OnMyEntity:
                break;
            case FlySubitemInitPositionEnum.OnRivalsEntity:
                break;
            case FlySubitemInitPositionEnum.OnTargetPosition:
                break;
            case FlySubitemInitPositionEnum.OnUseCollisionPosition:
                break;
            case FlySubitemInitPositionEnum.OnUseParentHangpoint:
                break;
        }
        Position = initPosition;
        ObjHandle<LogicEntity> caster = FightGame.Instance.GetMyEntity(mLEntity.PlayerID);
        if(caster.IsValid)
        {
            mDirection = caster.handle.GetLogicComponent<LogicMoveComponent>().FaceDirection;
            mMoveSpeed = new TSVector2(mDirection * InitialSpeed, 0);
        }
        
    }

    
    public void DoUpdate(FP deltaTime)
    {
        if (mIsInit == false)
            return;

        mMoveSpeed += new TSVector2(mDirection * XAcceleration, YAcceleration);
        Position += mMoveSpeed;
    }

    public void Destroy()
    {

    }

    public void Serialize(EByte ms)
    {
        ms.WriteInt(AllowPenetrateThrowPositionRect ? 1 : 0);
        OnUserParentHangpoint.Serialize(ms);
        ms.WriteInt((int)FlySubitemInitPosition);
        if(FlySubitemInitPosition == FlySubitemInitPositionEnum.OnMyEntity || FlySubitemInitPosition == FlySubitemInitPositionEnum.OnRivalsEntity)
            Hangpoint.Serialize(ms);
        ms.WriteLong(InitialSpeed.RawValue);
        ms.WriteLong(XAcceleration.RawValue);
        ms.WriteLong(YAcceleration.RawValue);
        ms.WriteInt(AllowBounceBack ? 1 : 0);
        ms.WriteInt(AllowPenetrateThroughPositionRect ? 1 : 0);
        ms.WriteInt(AllowFlyOutsideScene ? 1 : 0);
    }

    public void Deserialize(EByte ms)
    {
        AllowPenetrateThrowPositionRect=ms.ReadInt()==1;
        OnUserParentHangpoint = new OnUserParentHangpointDef();
        OnUserParentHangpoint.Deserialize(ms);
        FlySubitemInitPosition = (FlySubitemInitPositionEnum)ms.ReadInt();
        if (FlySubitemInitPosition == FlySubitemInitPositionEnum.OnMyEntity || FlySubitemInitPosition == FlySubitemInitPositionEnum.OnRivalsEntity)
        {
            Hangpoint = new HangpointDef();
            Hangpoint.Deserialize(ms);
        }
        InitialSpeed = FP.FromRaw(ms.ReadLong());
        XAcceleration = FP.FromRaw(ms.ReadLong());
        YAcceleration = FP.FromRaw(ms.ReadLong());
        AllowBounceBack = ms.ReadInt() == 1;
        AllowPenetrateThroughPositionRect = ms.ReadInt() == 1;
        AllowFlyOutsideScene = ms.ReadInt() == 1;
        mIsInit = true;
    }
}
